{% extends 'base.html' %}

{% block title %}采购管理 - 维修管理系统{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2>采购管理</h2>
    <a href="{% url 'spare_parts:purchase_add' %}" class="btn btn-primary">
        <i class="bi bi-plus-circle"></i> 新建采购订单
    </a>
</div>

<!-- 状态过滤 -->
<div class="card mb-4">
    <div class="card-body">
        <form method="get" class="row g-3">
            <div class="col-md-4">
                <label class="form-label">订单状态</label>
                <select name="status" class="form-select" onchange="this.form.submit()">
                    <option value="">所有状态</option>
                    <option value="draft" {% if selected_status == 'draft' %}selected{% endif %}>草稿</option>
                    <option value="submitted" {% if selected_status == 'submitted' %}selected{% endif %}>已提交</option>
                    <option value="approved" {% if selected_status == 'approved' %}selected{% endif %}>已批准</option>
                    <option value="ordered" {% if selected_status == 'ordered' %}selected{% endif %}>已订购</option>
                    <option value="received" {% if selected_status == 'received' %}selected{% endif %}>已收货</option>
                    <option value="cancelled" {% if selected_status == 'cancelled' %}selected{% endif %}>已取消</option>
                </select>
            </div>
        </form>
    </div>
</div>

<!-- 采购订单列表 -->
<div class="card">
    <div class="card-body">
        {% if purchase_orders %}
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>订单编号</th>
                        <th>供应商</th>
                        <th>状态</th>
                        <th>总金额</th>
                        <th>申请人</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for order in purchase_orders %}
                    <tr>
                        <td>{{ order.order_number }}</td>
                        <td>{{ order.supplier.name }}</td>
                        <td>
                            <span class="badge 
                                {% if order.status == 'draft' %}bg-secondary
                                {% elif order.status == 'submitted' %}bg-warning
                                {% elif order.status == 'approved' %}bg-info
                                {% elif order.status == 'ordered' %}bg-primary
                                {% elif order.status == 'received' %}bg-success
                                {% elif order.status == 'cancelled' %}bg-danger
                                {% else %}bg-light text-dark{% endif %}">
                                {{ order.get_status_display }}
                            </span>
                        </td>
                        <td>¥{{ order.total_amount }}</td>
                        <td>{{ order.requested_by.username }}</td>
                        <td>{{ order.created_at|date:"Y-m-d H:i" }}</td>
                        <td>
                            <a href="{% url 'spare_parts:purchase_detail' order.pk %}" class="btn btn-sm btn-outline-primary">
                                查看
                            </a>
                            {% if order.status == 'draft' %}
                            <a href="{% url 'spare_parts:purchase_edit' order.pk %}" class="btn btn-sm btn-outline-secondary">
                                编辑
                            </a>
                            {% endif %}
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
        <div class="text-center py-5">
            <i class="bi bi-cart-x display-1 text-muted"></i>
            <h5 class="mt-3">暂无采购订单</h5>
            <p class="text-muted">点击"新建采购订单"开始创建第一个采购订单</p>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}
